/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.client;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.biz.rs.domain.resp.GetTechniqueResponse;
import com.xy.biz.rs.domain.req.GetTechniqueRequest;
import com.xy.biz.rs.domain.req.ListTechniqueRequest;
import com.xy.lang.util.ResultUtils;

/**
 *
 *
 * @author wwj
 * @date 2021-4-13
 */
public interface TechniqueClient {

	/**
	 * 查找一个
	 *
	 * @param request
	 * @return
	 */
	Result<GetTechniqueResponse> getTechnique(GetTechniqueRequest request);

	/**
	 * 查找多个
	 *
	 * @param collection 标识集合
	 * @return
	 */
	Result<List<GetTechniqueResponse>> listTechniqueById(Collection<Long> collection);

	/**
	 * 查找多个
	 *
	 * @param request 查找条件
	 * @return
	 */
	Result<List<GetTechniqueResponse>> listTechnique(ListTechniqueRequest request);

	/**
	 * 统计个数
	 *
	 * @param request 查找条件
	 * @return
	 */
	Result<Integer> countTechnique(ListTechniqueRequest request);

	/**
	 * 分页查找
	 *
	 * @param request 查找条件
	 * @return
	 */
	Result<Page<GetTechniqueResponse>> pageTechnique(ListTechniqueRequest request);

	default <T> void buildByTechnique(Collection<T> source,
								 Collection<Long> techniqueIdCollection,
								 Function<T, Long> techniqueIdGetter,
								 BiConsumer<T, GetTechniqueResponse> builder) {
		Objects.requireNonNull(techniqueIdCollection, "The userIdGetter is required.");
		Objects.requireNonNull(builder, "The builder is required.");
		if(source == null || source.size() == 0) {
			return;
		}
		if(techniqueIdCollection == null || techniqueIdCollection.size() == 0) {
			return;
		}
		Set<Long> techniqueIdSet = new HashSet<>(techniqueIdCollection);
		List<GetTechniqueResponse> techniqueResponses = ResultUtils.get(listTechniqueById(techniqueIdSet));
		if(techniqueResponses != null && techniqueResponses.size() > 0) {
			Map<Long, GetTechniqueResponse> techniqueResponseMap = techniqueResponses.stream().collect(Collectors.toMap(GetTechniqueResponse::getTechniqueId, e -> e));
			GetTechniqueResponse techniqueResponse;
			Long userId;
			for(T entity : source) {
				userId = techniqueIdGetter.apply(entity);
				techniqueResponse = techniqueResponseMap.get(userId);
				if(techniqueResponse != null) {
					builder.accept(entity, techniqueResponse);
				}
			}
		}
	}

}
